package com.faxsun.web.endpoint.entity;

import java.io.Serializable;
import java.util.Comparator;
import java.util.Date;

import org.springframework.util.StringUtils;

import com.faxsun.core.persist.entity.LivePO;
import com.faxsun.core.pojos.enums.ChannelStatusType;

/**
 * 频道JSON格式
 * 
 * @author songdragon 2015年6月25日
 *
 */
public class ChannelJSON implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7685201494681503567L;

	private long id;
	private String name;
	private String desc;
	private String url;
	private String image;
	private ChannelStatusType type;
	private String start;
	private String end;
	private boolean isSelected;
	
    private String statusString;
    private String yearMonthString;
    
	/**
	 * 
	 * @param live
	 * @param contextPath
	 *            context path,e.g.:/faxsun-info
	 * @return
	 */
	public static ChannelJSON build(LivePO live, String contextPath) {
		ChannelJSON json = new ChannelJSON();
		json.id = live.getId();
		json.name = live.getTopic();
		json.url = contextPath + "/live/broadcast/" + json.id;
		if (!StringUtils.isEmpty(live.getCoverImage())) {
			json.image = live.getCoverImage();
			if (!json.image.startsWith("http")
					&& !json.image.startsWith("https")
					&& !json.image.startsWith("/faxsun"))
				json.image = contextPath + json.image;
		} else {
			json.image = "";
		}
		json.desc = live.getDescription();
		json.start = live.getStartTimeString();
		json.yearMonthString = json.start.substring(0, 4) + "年" + json.start.substring(5, 7) + "月";	
		json.setSelected(live.isSelected());
		json.end = live.getEndTimeString();

		Date present = new Date(System.currentTimeMillis());
		if (live.getStartTime().after(present)) {
			json.type = ChannelStatusType.FUTURE;
			json.statusString = "预告";
		} else if (live.getEndTime().before(present)) {
			json.type = ChannelStatusType.PAST;
            json.statusString = "已结束";
		} else {
			json.type = ChannelStatusType.PRESENT;
            json.statusString = "正在直播";
		}

		return json;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public ChannelStatusType getType() {
		return type;
	}

	public void setType(ChannelStatusType type) {
		this.type = type;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

    public String getStatusString() {
        return statusString;
    }

    public void setStatusString(String statusString) {
        this.statusString = statusString;
    }

    public String getYearMonthString() {
        return yearMonthString;
    }

    public void setYearMonthString(String yearMonthString) {
        this.yearMonthString = yearMonthString;
    }

    public boolean isSelected() {
		return isSelected;
	}

	public void setSelected(boolean isSelected) {
		this.isSelected = isSelected;
	}

	public static class Comparators {

        public static Comparator<String> YEARMONTH_ASC = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int year1 = Integer.parseInt(o1.substring(0, 4));
                int month1 = Integer.parseInt(o1.substring(5, 7));
                int year2 = Integer.parseInt(o2.substring(0, 4));
                int month2 = Integer.parseInt(o2.substring(5, 7));
                
                return year2 == year1? (month1 - month2) : (year1 - year2);
            }
        };
    }  
    
}
